package cn.com.shadowless.xinning.view.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mengpeng.mphelper.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.FragmentTabAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.ChildResult;
import cn.com.shadowless.xinning.model.order.CompanyBudgetOrderInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;
import cn.com.shadowless.xinning.view.activity.TabActivity;

/**
 * The type Company budget fragment.
 *
 * @author sHadowLess
 */
public class CompanyBudgetFragment extends BaseFragment {

    /**
     * The Company order num.
     */
    @BindView(R.id.company_orderNum)
    TextView companyOrderNum;
    /**
     * The Company make date.
     */
    @BindView(R.id.company_makeDate)
    TextView companyMakeDate;
    /**
     * The Company marker.
     */
    @BindView(R.id.company_marker)
    TextView companyMarker;
    /**
     * The Company position.
     */
    @BindView(R.id.company_position)
    TextView companyPosition;
    /**
     * The Company app info company.
     */
    @BindView(R.id.company_appInfo_company)
    TextView companyAppInfoCompany;
    /**
     * The Company app info budget years.
     */
    @BindView(R.id.company_appInfo_BudgetYears)
    TextView companyAppInfoBudgetYears;
    /**
     * The Company app info version.
     */
    @BindView(R.id.company_appInfo_version)
    TextView companyAppInfoVersion;
    /**
     * The Company app info remark.
     */
    @BindView(R.id.company_appInfo_remark)
    TextView companyAppInfoRemark;
    /**
     * The Company check process.
     */
    @BindView(R.id.company_check_process)
    LinearLayout companyCheckProcess;
    /**
     * The Company tab.
     */
    @BindView(R.id.company_tab)
    RecyclerView companyTab;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_company_budget_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            String billCode = bundle.getString("billCode");
            initCompanyBudgetOrderInfo(orderId, userName, billCode);
        }
    }

    private void initCompanyBudgetOrderInfo(String orderId, String userName, String billCode) {
        OrderPresenter
                .getSingleton()
                .getCompanyBudgetOrderInfo(orderId, userName, new RequestCallBack<CompanyBudgetOrderInfo>() {
                    @Override
                    public void success(RequestResult<CompanyBudgetOrderInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                CompanyBudgetOrderInfo.DataBean dataBean = result.getResult().getData();
                                List<CompanyBudgetOrderInfo.DataBean.BudgetTypeListBean> budgetTypeListBeans = dataBean.getBudgetTypeList();
                                List<CompanyBudgetOrderInfo.DataBean.HistoryListBean> historyListBeans = dataBean.getHistoryList();

                                String id = dataBean.getId();
                                String createTimeString = dataBean.getCreateTimeStr();
                                String userName = dataBean.getUserName();
                                String userDepartmentName = dataBean.getUserDepartmentName();
                                String positionName = dataBean.getPositionName();
                                companyOrderNum.setText(id);
                                companyMakeDate.setText(createTimeString);
                                companyMarker.setText(userName);
                                companyPosition.setText(userDepartmentName + positionName);

                                String companyName = dataBean.getCompanyName();
                                String year = dataBean.getYear() + "";
                                String version = dataBean.getVersion();
                                companyAppInfoCompany.setText(companyName);
                                companyAppInfoBudgetYears.setText(year);
                                companyAppInfoVersion.setText(version);
                                for (CompanyBudgetOrderInfo.DataBean.BudgetTypeListBean budgetTypeListBean : budgetTypeListBeans) {
                                    String remark = budgetTypeListBean.getRemark();
                                    companyAppInfoRemark.setText(remark);
                                }

                                companyTab.setLayoutManager(new LinearLayoutManager(mActivity));
                                FragmentTabAdapter<CompanyBudgetOrderInfo.DataBean.BudgetTypeListBean> fragmentTabAdapter = new FragmentTabAdapter<>(R.layout.fragment_tab_item, budgetTypeListBeans);
                                fragmentTabAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                                    String budgetTypeName = budgetTypeListBeans.get(position).getBudgetTypeName();
                                    EventBus.getDefault().postSticky(
                                            ChildResult
                                                    .builder()
                                                    .billCode(billCode)
                                                    .data(budgetTypeListBeans.get(position).getBudgetNoDetailList())
                                                    .dataName(budgetTypeName)
                                                    .build()
                                    );
                                    startActivity(new Intent(mActivity, TabActivity.class));
                                });
                                companyTab.setAdapter(fragmentTabAdapter);
                                fragmentTabAdapter.notifyDataSetChanged();

                                for (CompanyBudgetOrderInfo.DataBean.HistoryListBean bean : historyListBeans) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_order_process_item, null, false);
                                    LinearLayout linearLayout = view.findViewById(R.id.general_check_Process_Item);
                                    TextView time = view.findViewById(R.id.general_check_Process_Time);
                                    TextView link = view.findViewById(R.id.general_check_Process_Link);
                                    TextView name = view.findViewById(R.id.general_check_Process_Name);
                                    TextView status = view.findViewById(R.id.general_check_Process_Status);
                                    String checkTime = bean.getCheckTimeStr();
                                    String checkAccountUserName = bean.getCheckAccountUserName();
                                    String processName = bean.getProcessName();
                                    String statusStr = bean.getStatusStr();
                                    time.setText(checkTime);
                                    name.setText(String.format("%s(%s)", checkAccountUserName, processName));
                                    status.setText(statusStr);
                                    if ("待审核".equals(statusStr)) {
                                        time.setTextColor(Color.parseColor("#4a80ef"));
                                        link.setTextColor(Color.parseColor("#4a80ef"));
                                        name.setTextColor(Color.parseColor("#4a80ef"));
                                        status.setTextColor(Color.parseColor("#4a80ef"));
                                    }
                                    companyCheckProcess.addView(linearLayout);
                                }
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
